package com.qq.spirit.inter;

import java.util.List;

import com.gargoylesoftware.htmlunit.util.NameValuePair;
import com.qq.spirit.GameUtil;
import com.qq.spirit.LoggerUtil;
import com.qq.spirit.LoginTask;
import com.qq.spirit.TaskManager;
import com.qq.spirit.UserCache;
import com.qq.spirit.model.XmlDataUtil;
import com.qq.spirit.model.XmlNodeData;

public abstract class AbstractActionAfterHttpRequest implements IActionAfterHttpRequest
{

	public String username;
	public TaskUnit taskUnit;

	@Override
	public void call(List<NameValuePair> headerList, String content)
	{
		try
		{
			XmlNodeData data = XmlDataUtil.convertXmlToDataModel(content);
			if (!data.isLoadSuc())
			{
				// 加载失败,重新提交一次吧
				TaskManager.addTask(taskUnit, GameUtil.randomInt(10));
			}
			String state = GameUtil.getResultState(data);
			if (state.equals("1"))
			{
				doCall(headerList, data);
			} else
			{
				logErrorInfo(data);
				if (state.equals("0"))
				{
					TaskManager.addTask(taskUnit, GameUtil.randomInt(10));
				} else if (state.equals("2"))
				{
					TaskManager.addTask(new LoginTask(username, taskUnit));
				} else if (state.equals("3"))
				{
					// 验证码模块
				} else
				{

				}
			}
		} catch (Exception ex)
		{

		} finally
		{

		}
	}

	/**
	 * 记录错误消息
	 * 
	 * @param data
	 */
	private void logErrorInfo(XmlNodeData data)
	{
		String state = GameUtil.getResultState(data);
		String exception = GameUtil.getResultException(data);
		String message = GameUtil.getResultMessage(data);
		LoggerUtil.info(UserCache.getGameName(username) +taskUnit.getClass().getName()+ " 返回码：" + state + ",消息值:" + message + ",异常码:" + exception);
	}

	public abstract void doCall(List<NameValuePair> headerList, XmlNodeData resultData);
}
